package com.pmt.feedplayer.AppNavigation

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.clickable
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.List
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import com.pmt.feedplayer.R
import com.pmt.feedplayer.ui.AudioPlayerViewModel
import com.pmt.feedplayer.ui.VideoListViewModel
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Surface

/**
 * 应用导航组件
 * 实现底部导航栏，包含三个标签页
 */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AppNavigation(viewModel: VideoListViewModel) {
    // 当前选中的标签页索引
    val (selectedTab, setSelectedTab) = rememberSaveable {
        androidx.compose.runtime.mutableStateOf(0)
    }
    
    // 创建音频播放器ViewModel
    val audioViewModel = androidx.lifecycle.viewmodel.compose.viewModel<AudioPlayerViewModel>()
    
    // 根据视频和音频状态决定是否隐藏导航栏
    val hideNavBarForVideo = viewModel.hideBottomNavBar.value
    val hideNavBarForAudio = audioViewModel.hideBottomNavBar.value
    val hideNavBar = (hideNavBarForVideo ?: false) || (hideNavBarForAudio ?: false)
    
    Scaffold(
        modifier = Modifier.fillMaxSize(),
        bottomBar = {
            if (!hideNavBar) {
                NavigationBar {
                    NavigationBarItem(
                        icon = { Icon(imageVector = Icons.Filled.List, contentDescription = "视频") },
                        label = { Text("视频") },
                        selected = selectedTab == 0,
                        onClick = { setSelectedTab(0) }
                    )
                    NavigationBarItem(
                        icon = { Icon(imageVector = Icons.Filled.List, contentDescription = "音频") },
                        label = { Text("音频") },
                        selected = selectedTab == 1,
                        onClick = { setSelectedTab(1) }
                    )
                    NavigationBarItem(
                        icon = { Icon(imageVector = Icons.Filled.Settings, contentDescription = "设置") },
                        label = { Text("设置") },
                        selected = selectedTab == 2,
                        onClick = { setSelectedTab(2) }
                    )
                }
            }
        },
        topBar = {
            // 当视频播放器可见时，隐藏顶部AppBar
            if (!viewModel.showPlayer.value) {
                androidx.compose.material3.TopAppBar(
                    title = {
                        Text(
                            when (selectedTab) {
                                0 -> "本地视频"
                                1 -> "音频列表"
                                else -> "设置"
                            }
                        )
                    }
                )
            }
        }
    ) { paddingValues ->
        Surface(modifier = Modifier.fillMaxSize().padding(if (viewModel.showPlayer.value == true) androidx.compose.foundation.layout.PaddingValues() else paddingValues)) {
            // 根据选中的标签页显示对应的内容
            when (selectedTab) {
                0 -> VideoTabContent(viewModel = viewModel)
                1 -> AudioTabContent(audioPlayerViewModel = audioViewModel)
                2 -> SettingsTabContent(viewModel = viewModel)
            }
            
            // 如果视频播放器可见，全屏显示视频播放器
            if (viewModel.showPlayer.value == true) {
                Box(modifier = Modifier.fillMaxSize()) {
                    Text(text = "视频播放器", 
                         modifier = Modifier
                             .fillMaxSize()
                             .clickable { viewModel.closePlayer() }
                             .wrapContentSize(Alignment.Center))
                }
            }
        }
    }
}